package com.oocl.pmsystem.bean;

/**
 * 命令解析器
 * 1. 类型
 *   `L`  直接查看所有数据
 *   `A`  
 *   `D`
 *   `U`
 *   `O`
 *   `H`
 *   `Q`
 * @author WUJA13
 *
 */
public class Command {

	///命令头： `L` ， `A`， `D`， `U` 等
	private String commandHead = null;
	
	///命令选项
	private String[] commandOptions = null; 
	
	///命令头之后的内容
	///键，如`L name:ABC` 中的`name`
	private String[] commandKeys = null;
	///值，如`L name:ABC` 中的`ABC`
	private String[] commandValues = null;
	
	private static final String MATCH_A = "\\s*:\\s*";///匹配 ` : `
	private static final String MATCH_SPACE = "\\s+";///匹配 空格
	private static final String MATCH_B = "\\s*,\\s*";//匹配 ` , `
	private static final String MATCH_D = "^[LADUOHQ]\\s*";/// 只有 命令头的命令
	private static final String MATCH_E = "^[LADUOHQ]\\s+(\\s*[^\\s]+\\s*)+";///带有 key:name 或 key 的 命令头
	private static final String MATCH_F = "^[LADUOHQ]\\s+(\\s*[^\\s:;]+\\s*)+";///带有 option 的命令头
	
	public Command(String command){
		///将普通的命令，转换为系统管理员可读的指令
		if(command== null || command.length()==0)
//			throw new RuntimeException("Input command's length can't be Zero");
			this.commandHead = "Non";
		if(command.matches(MATCH_D)){/// 只有 命令头的命令
			this.commandHead = command.trim();
		}
		else if(command.matches(MATCH_F)){///命令头 + 命令选项
			String[] headAndOptions = command.trim().split(MATCH_SPACE);
			if(headAndOptions.length==0)
				return;
			else{
				this.commandHead = headAndOptions[0];
				this.commandOptions = new String[headAndOptions.length-1];
				for(int i=1;i<headAndOptions.length;i++)
					this.commandOptions[i-1] = headAndOptions[i];
			}
		}
		else if(command.matches(MATCH_E)){///带有 key:name 或 key 的 命令头
			String[] keyMaps = command.split(MATCH_B);
			String[] headAndFirstKeyMap = keyMaps[0].split(MATCH_SPACE);
			///拿到命令头
			this.commandHead = headAndFirstKeyMap[0];
			
			///获取命令选项
			///如果有 非命令头+非key:value 的命令，则 说明是： 命令选项
			if(headAndFirstKeyMap.length>2){
				this.commandOptions = new String[headAndFirstKeyMap.length-2];
				for(int i=1;i<headAndFirstKeyMap.length-1;i++)
					this.commandOptions[i-1] = headAndFirstKeyMap[i];
			}
			
			///开始拿key:value
			///首先，判断key：value 长度是否>1
			if(keyMaps.length<1)return;
			
			this.commandKeys =   new String[keyMaps.length];
			this.commandValues = new String[keyMaps.length];
			///第一个key:value
			String[] firstKeyMap =  headAndFirstKeyMap[headAndFirstKeyMap.length-1].split(MATCH_A);
			if(firstKeyMap.length>=1)
			this.commandKeys[0] = firstKeyMap[0];
			if(firstKeyMap.length>=2)
			this.commandValues[0] = firstKeyMap[1];
			///剩下的key:value
			String[] itemKeyMap;
			for(int i=1;i<keyMaps.length;i++){
				itemKeyMap = keyMaps[i].split(MATCH_A);
				if(itemKeyMap.length>=1)
				this.commandKeys[i] = itemKeyMap[0];
				if(itemKeyMap.length>=2)
				this.commandValues[i] = itemKeyMap[1];
			}
		}else{
			this.commandHead="Non";///什么都不是
		}
	}
	
	
	public String getCommandHead() {
		return commandHead;
	}
	
	
	public void setCommandHead(String commandHead) {
		this.commandHead = commandHead;
	}
	public String[] getCommandKeys() {
		return commandKeys;
	}
	public void setCommandKeys(String[] commandKeys) {
		this.commandKeys = commandKeys;
	}
	public String[] getCommandValues() {
		return commandValues;
	}
	public void setCommandValues(String[] commandValues) {
		this.commandValues = commandValues;
	}
	
	
	
	public String[] getCommandOptions() {
		return commandOptions;
	}


	public void setCommandOptions(String[] commandOptions) {
		this.commandOptions = commandOptions;
	}


	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb
		.append("Head: ")
		.append(this.commandHead==null ? "Null":this.commandHead)
		.append("\r\n Options: ")
		.append(showCommandOptions())
		.append("\r\n Keys: ")
		.append(showCommandKeys())
		.append("\r\n Vals: ")
		.append(showCommandValues());
		return sb.toString();
	}
	
	////打印 所有`键`
	private String showCommandKeys(){
		if(this.commandKeys==null || this.commandKeys.length==0)
			return "Null";
		else{
			StringBuilder sb =new StringBuilder();
			sb.append("{ ");
			for(String item:this.commandKeys){
				sb.append(item)
				.append(",");
			}
			sb.deleteCharAt(sb.length()-1).append(" }");
			return sb.toString();
		}
	}
	
////打印 所有`命令选项`
	private String showCommandOptions(){
		if(this.commandOptions==null || this.commandOptions.length==0)
			return "Null";
		else{
			StringBuilder sb =new StringBuilder();
			sb.append("{ ");
			for(String item:this.commandOptions){
				sb.append(item)
				.append(",");
			}
			sb.deleteCharAt(sb.length()-1).append(" }");
			return sb.toString();
		}
	}
	
	////打印 所有`值`
	private String showCommandValues(){
		if(this.commandValues==null || this.commandValues.length==0)
			return "Null";
		else{
			StringBuilder sb =new StringBuilder();
			sb.append("{ ");
			for(String item:this.commandValues){
				sb.append(item)
				.append(",");
			}
			sb.deleteCharAt(sb.length()-1).append(" }");
			return sb.toString();
		}
	}
	
	
}
